package fr.cephb.berkeleydb.binding;

import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

public class IStringBinding
	extends XTupleBinding<String>
	{
	public static final byte TO_LOWERCASE=0;
	public static final byte TO_UPPERCASE=1;
	private byte type=TO_LOWERCASE;
	
	public IStringBinding()
		{
		this(TO_LOWERCASE);
		}
	
	public IStringBinding(byte type)
		{
		this.type=type;
		}

	@Override
	public String entryToObject(TupleInput in)
		{
		return convert(in.readString());
		}

	@Override
	public void objectToEntry(String s, TupleOutput out)
		{
		out.writeString(convert(s));
		}
	
	private String convert(String s)
		{
		return s==null?null:
			this.type==TO_LOWERCASE?s.toLowerCase():
			s.toUpperCase();
		}
	}
